package com.eco.mam.work.request.transform;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Iterator;

import javanet.staxutils.IndentingXMLStreamWriter;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import org.apache.log4j.Logger;

import com.eco.mam.work.ConfigParameters;
import com.eco.mam.work.DataException;
import com.eco.mam.work.request.WorkRequestProcessHelper;
import com.eco.mam.work.request.dto.MeterPoiint;
import com.eco.mam.work.request.dto.Transaction;
import com.eco.mam.work.request.dto.WorkRequest;
import com.eco.mam.work.request.resource.WorkRequestConfig;

/*Helps XMLTransformer to achieve xml transformation*/
public class XMLTransformHelper {
    static final Logger logger = Logger
            .getLogger(WorkRequestProcessHelper.class);
    private StringWriter stringWriter = null;
    private XMLStreamWriter writer = null;
    private XMLOutputFactory outputFactory = null;

    /* Returns an XMLstreamWriter stream */
    public XMLStreamWriter getXMLStreamWriter() throws DataException {
        logger.info("Entering XMLTransformHelper  getXMLStringWriter() ");
        stringWriter = new StringWriter();
        outputFactory = XMLOutputFactory.newInstance();
        try {
            writer = outputFactory.createXMLStreamWriter(stringWriter);
        } catch (XMLStreamException e) {

            throw new DataException(
                    "Exception happened ,Can not create an XMlStream to wrap  StringWriter",
                    e);
        }
        return writer;
    }

    /* Returns an XMLstreamWriter stream */
    public XMLStreamWriter getXMLStreamWriter(String strFileName)
            throws DataException {
        logger.info("Entering XMLTransformHelper  getXMLStreamWriter() ");
        // String strFileName=null;
        try {

            if (strFileName != null) {

                if (strFileName.length() != 0 || !strFileName.isEmpty()) {
                    File file = new File(strFileName);
                    if (!file.exists()) {
                        file.createNewFile();

                    }
                    if (file.exists()) {
                        FileOutputStream fileOutputStream = new FileOutputStream(
                                file);
                        outputFactory = XMLOutputFactory.newInstance();
                        writer = outputFactory
                                .createXMLStreamWriter(fileOutputStream);

                        if (logger.isDebugEnabled()) {
                            logger.debug("Output Stream  is  :"
                                    + fileOutputStream);
                            logger.debug("XMLStreamWriter  is :" + writer);
                        }
                    }
                }
            }
            if (strFileName == null || strFileName.length() == 0
                    || strFileName.isEmpty()) {

                logger.debug("WorkRequestConfig.getFileName(strXMLOutPutFile)  returns  :"
                        + WorkRequestConfig
                                .getFileName(ConfigParameters.XML_OUTPUT_FILE));
                throw new IllegalArgumentException(
                        "The argument passed is illegal ,argument is  :"
                                + strFileName);
            }
        } catch (IOException e) {
            {
                throw new DataException(
                        "Error happened,can not create or access file", e);

            }

        } catch (XMLStreamException e) {
            throw new DataException(
                    "Error happened,can not create or access file :"
                            + strFileName, e);
        }

        return writer;

      
    }
}
